package org.jeecg.modules.basic.entry.util;



import java.nio.charset.Charset;
import java.util.Map;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

/**
 * 该方法为使用HttpClient调用webservice接口
 * @author LG
 *
 */
public class wsCallService {
    private static String namespace = "http://LwDkService.asmx/";

    public static String callServiceHC(String wsUrl, String xq,String fj,String xl) {
        String xml = createSoapContent(xq,fj,xl);
        String returnDatabase = doPostSoap(wsUrl, xml, "");
        return returnDatabase;
    }

    /**
     * 根据拼接 xml 字符串
     * @return
     */
    public static String createSoapContent(String xq,String fj,String xl) {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:lwd=\"http://LwDkService.asmx/\">\n");
        stringBuilder.append("<soapenv:Header/>\n");
        stringBuilder.append("<soapenv:Body>\n");
        stringBuilder.append("<lwd:TraceDlZTArray>\n");
        stringBuilder.append("<lwd:inMm>60C85273A0D8834B798C5FA766D76B866B609AA755C3584F797F798C7FF8785D</lwd:inMm>\n");
        stringBuilder.append("<lwd:inXq>"+xq+"</lwd:inXq>\n");
        stringBuilder.append("<lwd:inFj>"+fj+"</lwd:inFj>\n");
        stringBuilder.append("<lwd:cxLx>"+xl+"</lwd:cxLx>\n");
        stringBuilder.append("</lwd:TraceDlZTArray>\n");
        stringBuilder.append("</soapenv:Body>\n");
        stringBuilder.append("</soapenv:Envelope>");
        return stringBuilder.toString();
    }

    /****************** 电量设置 Start ******************/

    public static String callDkService(String wsUrl,String xq, String fj, String dl, String id,String lsh,String lx) {
        String xml = setDlDataContent(xq,fj,dl,id,lsh,lx);
        String returnDatabase = doPostSoap(wsUrl, xml, "");
        return returnDatabase;
    }
    /** 免费和收费电量设置
     * 根据拼接 xml 字符串
     * @return
     */
    public static String setDlDataContent(String xq, String fj, String dl, String id,String lsh,String lx) {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:lwd=\"http://LwDkService.asmx/\">\n");
        stringBuilder.append("<soapenv:Header/>\n");
        stringBuilder.append("<soapenv:Body>\n");
        stringBuilder.append("<lwd:SetDLData>\n");
        stringBuilder.append("<lwd:inMm>7A0E6BC9978E804D77047953B54F7AA8863E71671E891E99</lwd:inMm>\n");
        stringBuilder.append("<lwd:inXq>"+xq+"</lwd:inXq>\n");
        stringBuilder.append("<lwd:inFj>"+fj+"</lwd:inFj>\n");
        stringBuilder.append("<lwd:inDl>"+dl+"</lwd:inDl>\n");
        stringBuilder.append("<lwd:inID>"+id+"</lwd:inID>\n");
        stringBuilder.append("<lwd:inLsh>"+lsh+"</lwd:inLsh>\n");
        stringBuilder.append("<lwd:inLx>"+lx+"</lwd:inLx>\n");
        stringBuilder.append("</lwd:SetDLData>\n");
        stringBuilder.append("</soapenv:Body>\n");
        stringBuilder.append("</soapenv:Envelope>");
        return stringBuilder.toString();
    }
    /****************** 电量设置 End ******************/

    /**
     * HTTPClient 调用 WebService
     * @param url
     * @param soap
     * @param SOAPAction
     * @return
     */
    public static String  doPostSoap(String url, String soap, String SOAPAction) {
        //请求体
        String retStr = "";
        // 创建HttpClientBuilder
        HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
        // HttpClient
        CloseableHttpClient closeableHttpClient = httpClientBuilder.build();
        HttpPost httpPost = new HttpPost(url);
        try {
            httpPost.setHeader("Content-Type", "text/xml;charset=UTF-8");
            //httpPost.setHeader("SOAPAction", SOAPAction);
            StringEntity data = new StringEntity(soap,
                    Charset.forName("UTF-8"));
            httpPost.setEntity(data);
            CloseableHttpResponse response = closeableHttpClient
                    .execute(httpPost);
            HttpEntity httpEntity = response.getEntity();
            if (httpEntity != null) {
                // 打印响应内容
                retStr = EntityUtils.toString(httpEntity, "UTF-8");
            }
            // 释放资源
            closeableHttpClient.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return retStr;
    }
}

